<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ACM刷题统计</title>
<script type="text/javascript" src="./js/oj.js"></script>
<script type="text/javascript" src="./js/acmer.js"></script>
<script type="text/javascript" src="./js/storage.js"></script>
<script type="text/javascript">
function onload(){
	//get info from localstorage
	storage.acmers.get_user_list('init');
	
	//update acmer's info
	setInterval(function(){
		storage.acmers.update_oj();
	},1200000);
	storage.acmers.update_oj();
}
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
		var i;
		switch(request.hope)
		{
		case "update_oj":
			if (storage.acmers.status == "loading")
				sendResponse({"status":"loading"})
			else{
				storage.acmers.update_oj();
				sendResponse({});
			}
			break;
		case "update_acmer":
			for (i = 0;i < storage.acmers.list.length;i++){
				if (storage.acmers.list[i].uid == request.uid){
					storage.acmers.list[i].update("hdu");
					storage.acmers.list[i].update("pku");
					storage.acmers.list[i].update("bistu");
					sendResponse({});
					break;
				}
			}
			break;
		case "get_acmer_list":
			sendResponse(storage.acmers.list);
			break;
		case "save_acmer_list":
			try{
				localStorage['acmers_list'] = JSON.stringify(request.acmer_list);
				storage.acmers.get_user_list();
				if (request.update_oj == "1")
					storage.acmers.update_oj();
			}catch(e){
				console.log(e);
			}	
			sendResponse({});
			break;
		}
		
	}
 );

chrome.browserAction.onClicked.addListener(function(thisTab) { 
	window.open('rank.html','','');
});

</script>
</head>
<body onload= "onload()">
</body>
</html>
